
package android.mcsmtms.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.mcsmtms.constant.GlobalConstants.GPSConstants;

public class AppSettings {
	private static final String PREFERENCE_NAME = "MCDRRMO_PREFERENCES";
	private static final String SERVER_NUMBER = "SERVER_NUMBER";
	private static final String SERVER_URL = "SERVER_URL";
	private static final String MEASUREMENT_UNIT = "MEASUREMENT_UNIT";
	private static final String MIN_SPEED_PERCENTAGE = "MIN_SPEED_PERCENTAGE";
	private static final String KMH_UNIT_STR = "km/h";
	private static final String M_UNIT_STR = "m/h";
	private static final String MI_UNIT_STR = "mi/h";
	
	public static int getMeasureUnit(Context context){
		int measureUnit = getInt(context,AppSettings.MEASUREMENT_UNIT);
		return measureUnit;
	}
	
	public static String getMeasureUnitString (Context context){
		String string = "";
		int measureUnit = getInt(context,AppSettings.MEASUREMENT_UNIT);
		
		switch(measureUnit)
		{
			case GPSConstants.INDEX_M:		string = "m/h";	break; //for testing purposes
			case GPSConstants.INDEX_KM:		string = "km/h";	break;
			case GPSConstants.INDEX_MI:		string = "mi/h";	break;
		}
		
		return string;
	}
	
	public static void setMeasureUnit(Context context,int limit){
		putInt(context,AppSettings.MEASUREMENT_UNIT,limit);
	}
	
	public static void setMeasureUnit(Context context,String unitStr){
		if(M_UNIT_STR.equalsIgnoreCase(unitStr)) {
			setMeasureUnit(context,GPSConstants.INDEX_M);
		} else if(KMH_UNIT_STR.equalsIgnoreCase(unitStr)) {
			setMeasureUnit(context,GPSConstants.INDEX_KM);
		} else if(MI_UNIT_STR.equalsIgnoreCase(unitStr)) {
			setMeasureUnit(context,GPSConstants.INDEX_MI);
		}
	}
	
	public static String getServerUrl(Context context){
		return getString(context,AppSettings.SERVER_URL);
	}
	
	public static void setServerUrl(Context context, String url){
		putString(context,AppSettings.SERVER_URL,url);
	}
	
	public static String getServerNumber(Context context){
		return getString(context,AppSettings.SERVER_NUMBER);
	}
	
	public static void setServerNumber(Context context, String number){
		putString(context,AppSettings.SERVER_NUMBER,number);
	}
	
	public static void setMinSpeedPercentage(Context context, int value) {
		putInt(context, MIN_SPEED_PERCENTAGE, value);
	}
	
	public static double getMinSpeedPercentage(Context context) {
		return getInt(context, MIN_SPEED_PERCENTAGE) * .01;
	}
	
	public static int getMinSpeedPercentageInt(Context context) {
		return getInt(context, MIN_SPEED_PERCENTAGE);
	}
	
	private static int getInt(Context context, String tag) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);

		return pref.getInt(tag, 0);
	}
	
	public static void putInt(Context context, String tag, int value) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
		SharedPreferences.Editor editor = pref.edit();

		editor.putInt(tag, value);
		editor.commit();
	}
	
	private static String getString(Context context, String tag) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);

		return pref.getString(tag, "");
	}
	
	public static void putString(Context context, String tag, String value) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
		SharedPreferences.Editor editor = pref.edit();

		editor.putString(tag, value);
		editor.commit();
	}
}
